package cn.platform.service.impl;

import cn.platform.entity.Attendance;
import cn.platform.mapper.AttendanceMapper;
import cn.platform.request.AttendanceQueryDTO;
import cn.platform.request.AttendanceRecordDTO;
import cn.platform.response.ActivityVO;
import cn.platform.response.AttendanceVO;
import cn.platform.response.PageResult;
import cn.platform.service.AttendanceService;
import cn.platform.util.AttendanceWebSocketHandler;
import cn.platform.util.identify.SessionToken;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private AttendanceWebSocketHandler attendanceWebSocketHandler;
    @Override
    public void recordAttendance(AttendanceRecordDTO attendanceRecord) throws JsonProcessingException {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=dateFormat.format(date);

        LocalDateTime now = LocalDateTime.now();
        Attendance attendance = new Attendance();
        attendance.setStuID(attendanceRecord.getStudentId());
        attendance.setCheckType(attendanceRecord.getCheckType());

        attendance.setCheckTime(now);
        attendanceRecord.setCheckTime(time);

        LocalDate currentDate =LocalDate.now();

        LocalTime startTime = LocalTime.of(6,0);
        LocalTime endTime = LocalTime.of(8,20);


        LocalDateTime startDateTime = currentDate.atTime(startTime);
        LocalDateTime endDateTime = currentDate.atTime(endTime);
        if(attendanceRecord.getCheckType().equals("进校")){
            if(now.isBefore(startDateTime)||now.isAfter(endDateTime)){
                attendance.setIsOverdue("是");
            }else {
                attendance.setIsOverdue("否");
            }
        }else {
            attendance.setIsOverdue("否");
        }

        attendanceMapper.insert(attendance);
        attendanceWebSocketHandler.sendMessageToClient(attendanceRecord);

    }

    @Override
    public PageResult<AttendanceVO> page(AttendanceQueryDTO attendanceQueryDTO) {
        if(attendanceQueryDTO.getGrade()!=null){
            attendanceQueryDTO.setGradeYear(Year.now().getValue()-attendanceQueryDTO.getGrade()+1);
        }
        PageHelper.startPage(attendanceQueryDTO.getPage(),attendanceQueryDTO.getPageSize());
        Page<AttendanceVO> voList = attendanceMapper.page(attendanceQueryDTO);

        int currentYear = Year.now().getValue();
        for(AttendanceVO vo:voList){
            int gradeNum = currentYear-vo.getGradeYear()+1;
            if (gradeNum>=1 && gradeNum<=6){
                vo.setGrade(gradeNum);
            }else {
                vo.setGrade(null);
                vo.setClassNum(null);
            }
        }

        return new PageResult<>(voList.getTotal(),voList.getResult());
    }

    @Override
    public void deleteRecord(Integer id) {
        attendanceMapper.deleteRecord(id);
    }

    @Override
    public void deleteBatchRecords(List<Integer> ids) {
        attendanceMapper.deleteBatch(ids);
    }
}
